<!doctype html>
<html>
  <head>
    <link rel='stylesheet' href='/resources/testharness.css'>
    <script>
      (function() {
      var testURLs = [];
      var testIndex = 0;
      var testCount = 0;
      var STATUS_STRINGS = ["PASS", "FAIL", "TIMEOUT", "NOTRUN"];

      function onMessage(e)
      {
          var testData = e.data;

          //console.log(testData.type, testData);

          if (testData.type == "complete") {
              var tableBody = document.querySelector("#results tbody");
              var fragment = document.createDocumentFragment();

              testCount += testData.tests.length;
	      
              for (var i = 0; i < testData.tests.length; ++i) {
	          var testResults = testData.tests[i];
		  var tr = document.createElement("tr");
		  var status = document.createElement("td");
		  var testName = document.createElement("td");
                  var message = document.createElement("td");

		  status.textContent = STATUS_STRINGS[testResults.status];
                  tr.className = STATUS_STRINGS[testResults.status].toLowerCase();

		  testName.textContent = testResults.name;
                  message.textContent = testResults.message;

		  tr.appendChild(status);
		  tr.appendChild(testName);
                  tr.appendChild(message);
		  fragment.appendChild(tr);
              }

	      testIndex++;
	      if (testIndex >= testURLs.length) {
	    	  return;
	      }

              tableBody.appendChild(fragment);
	      startNextTest();
              return;
          }
      }

      function startNextTest()
      {
	  var iframe = document.querySelector("iframe");
          var testURL = testURLs[testIndex];

	  var tr = document.createElement("tr");
	  var td = document.createElement("td");
	  td.appendChild(document.createTextNode("Running tests in '" + testURL + "' :"));
	  td.colSpan = 3;
	  tr.appendChild(td);
	  document.querySelector("#results tbody").appendChild(tr);

  	  iframe.src = testURL;
      }

      window["onBodyLoad"] = function()
      {
          window.addEventListener("message", onMessage);
          var xhr = new XMLHttpRequest();
          xhr.open("GET", "manifest.txt", false);
          xhr.send();

          var str = xhr.response.replace("\r", "");
          testURLs = xhr.response.replace("\r", "").split("\n");
          startNextTest();
      }

      
      })(window);
    </script>
  </head>
  <body onload="onBodyLoad()">
    <iframe></iframe>
    <table id="results" style="float:left;">
      <tbody>
      </tbody>
    </table>
    <div id="log"></div>
  </body>
</html>
